<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :xs="15" :sm="18" :md="19" :lg="20" :xl="20">
        <!--工具栏-->
        <div class="contents">
          <div class="head-container">
            <div class="center_f" v-show="isShow">
              <!-- 搜索 -->
              <div class="box_si">
                <el-input
                  placeholder="请输入姓名"
                  v-model="input"
                  clearable>
                </el-input>
              </div>
              <!--日期-->
              <div class="block">
                <el-date-picker
                  v-model="dictDate"
                  type="daterange"
                  align="right"
                  unlink-panels
                  range-separator=":"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="yyyy-MM-dd"
                  :picker-options="pickerOptions">
                </el-date-picker>
              </div>
              <el-button type="success" icon="el-icon-search" size="small" class="box_ss">搜索</el-button>
              <el-button type="warning" icon="el-icon-refresh" size="small" @click="resetForm()">重置</el-button>
            </div>
            <!--          增删改-->
            <div class="list_box_c">
              <span class="list_box_left">
                <el-button type="primary" size="small" icon="el-icon-plus" @click="addMembers">新增</el-button>
                <el-button type="danger" disabled size="small" icon="el-icon-delete">删除</el-button>
                <el-button type="info" size="small" icon="el-icon-help">重载数据字典</el-button>
                <el-button type="success" size="small" icon="el-icon-edit">同步数据字典</el-button>
                <el-button type="warning" size="small" icon="el-icon-download">导出</el-button>
              </span>
              <div class="list_box_right">
                <el-button icon="el-icon-search" size="small" @click="changeIsShow()"></el-button>
                <el-button icon="el-icon-refresh" size="small" @click="refresh()"></el-button>
              </div>
            </div>
            <!--          表格-->
            <div class="content-bottom">
              <el-table
                ref="multipleTable"
                tooltip-effect="dark"
                :data="Dict"
                @row-click="queryDictData"
                style="width: 100%">
                <el-table-column
                  type="selection"
                  width="55">
                </el-table-column>
                <el-table-column
                  type="index"
                  label="序号">
                </el-table-column>
                <el-table-column
                  prop="dictionary_name"
                  label="类型名称">
                </el-table-column>
                <el-table-column
                  prop="dictionary_code"
                  label="代码">
                </el-table-column>
                <el-table-column
                  prop="create_by_name"
                  label="创建人">
                </el-table-column>
                <el-table-column
                  prop="create_time"
                  label="创建日期">
                </el-table-column>
                <el-table-column
                  prop="address"
                  label="操作"
                  width="150px">
                  <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" circle @click="modifyData(scope.$index, scope.row)"></el-button>
                    <el-button class="btn_x" size="medium" type="text" icon="el-icon-delete" @click="removeDict(scope.row.dictionary_id)"></el-button>
                  </template>
                </el-table-column>
              </el-table>
              <!--分页组件-->
              <pagination
                :total="totalCount"
                :pageNum="pageNum"
                :pageSize="pageSize"
                @pageChange="pageChange"
              />
            </div>
          </div>
          <div class="center-right">
            <div class="content-top">
              <h3>字典详情</h3>
              <el-button type="primary" size="mini" icon="el-icon-plus">新增</el-button>
            </div>
            <div class="content-text">
              <el-table
                ref="multipleTable"
                tooltip-effect="dark"
                :data="tableData"
                style="width: 100%">
                <el-table-column
                  prop="index"
                  label="序号">
                </el-table-column>
                <el-table-column
                  prop="dictionary_name"
                  label="字典名称">
                </el-table-column>
                <el-table-column
                  prop="dictionary_code"
                  label="字典值">
                </el-table-column>
                <el-table-column
                  prop="create_by_name"
                  label="创建人">
                </el-table-column>
                <el-table-column
                  prop="create_time"
                  label="创建日期">
                </el-table-column>
                <el-table-column
                  prop="address"
                  label="操作">
                  <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" circle></el-button>
                    <el-button class="btn_x" size="medium" type="text" icon="el-icon-delete"></el-button>
                  </template>
                </el-table-column>
              </el-table>
              <!--分页组件-->
              <pagination
                :total="totalCount"
                :pageNum="pageNum"
                :pageSize="pageSize"
                @pageChange="pageChange"
              />
            </div>
          </div>
        </div>
<!--        新增用户-->
        <el-dialog title="新增用户" :visible.sync="isAddMembers" width="30%" style="font-weight: bold">
          <el-form :model="addForm" ref="addForm" class="demo-ruleForm" label-width="70px">
            <el-form-item label="类型名称" prop="dictionary_name">
              <div class="center-box" style="display: flex">
                <el-input v-model="addForm.dictionary_name"></el-input>
              </div>
            </el-form-item>
            <el-form-item label="代码" prop="dictionary_code">
              <div class="center-box" style="display: flex">
                <el-input v-model="addForm.dictionary_code"></el-input>
              </div>
            </el-form-item>
            <el-form-item label="创建人" prop="create_by_name">
              <div class="center-box" style="display: flex">
                <el-input v-model="addForm.create_by_name"></el-input>
              </div>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="closeDialog()">取 消</el-button>
            <el-button type="primary" @click="submitForm()">确 定</el-button>
          </span>
        </el-dialog>
        <!--        编辑用户-->
        <el-dialog title="新增用户" :visible.sync="centerDialogVisible" width="30%" style="font-weight: bold">
          <el-form :model="updateForm" ref="updateForm" class="demo-ruleForm" label-width="70px">
            <el-form-item label="类型名称" prop="dictionary_name">
              <div class="center-box" style="display: flex">
                <el-input v-model="updateForm.dictionary_name"></el-input>
              </div>
            </el-form-item>
            <el-form-item label="代码" prop="dictionary_code">
              <div class="center-box" style="display: flex">
                <el-input v-model="updateForm.dictionary_code"></el-input>
              </div>
            </el-form-item>
            <el-form-item label="创建人" prop="create_by_name">
              <div class="center-box" style="display: flex">
                <el-input v-model="updateForm.create_by_name"></el-input>
              </div>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="closeDialog()">取 消</el-button>
            <el-button type="primary" @click="sumbitEditRow()">确 定</el-button>
          </span>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>
<script>
  import pagination from '../../../pagination/pagination'
  export default {
    inject: ['reload'],
    components: { pagination },
    data () {
      return {
        // 控制修改用户对话框的显示与隐藏
        centerDialogVisible: false,
        // 控制添加用户对话框的显示与隐藏
        isAddMembers: false,
        // 搜索的绑定
        input: '',
        // 当前页码
        pageNum: 0,
        // 默认每页数据量
        pageSize: 5,
        // 共有多少条数据
        totalCount: 0,
        // 表格
        Dict: [],
        tableData: [],
        dictionary_id: '',
        // 新增的参数为空
        addForm: {},
        // 修改的参数
        updateForm: {},
        // 搜索框显示或隐藏
        isShow: true,
        istrue: '',
        isvalidate: '',
        sysUser: '',
        // 日期
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        },
        dictDate: ''
      }
    },
    created () {
      this.queryDict()
    },
    methods: {
      // 查询
      queryDict() {
        this.$axios.get('http://localhost:8443/DictMenus').then(resp => {
          if (resp && resp.status === 200) {
            this.Dict = resp.data
          }
        })
      },
      // 获取当前行的id
      queryDictData(row) {
        this.dictionary_id = row.dictionary_id
        console.log(this.dictionary_id)
      },
      /**
       * @author hpx
       * @date 2021/9/24 8:58
       * 点击新增弹框打开
       */
      addMembers() {
        this.isAddMembers = true
        this.addForm = {}
      },
      /**
       * @author hpx
       * @date 2021/9/24 8:58
       * 点击编辑时弹框打开
       */
      modifyData(index, row) {
        this.centerDialogVisible = true
        this.updateForm = Object.assign({}, row) // 重置对象
      },
      /**
       * @author hpx
       * @date 2021/9/24 8:58
       * 重置搜索框，日期
       */
      resetForm: function () {
        this.input = ''
        this.dictDate = ''
      },
      /**
       * @author hpx
       * @date 2021/9/24 8:58
       * 分页
       */
      pageChange(page) {
        this.pageSize = page.pageSize
        this.pageNum = page.currentPage
      },
      /**
       * @author hpx
       * @date 2021/9/24 8:58
       * 保存并提交新增数据
       */
      submitForm() {
        if (this.addForm.dictionary_name === '' || this.addForm.dictionary_code === '' || this.addForm.create_by_name === '') { // 非空校验
          this.$confirm('请输入完整内容?', '提示', {
            type: 'warning'
          })
        } else {
          this.$axios
            .post('http://localhost:8443/addDict', this.addForm).then(resp => {
            this.istrue = resp.data
            if (this.istrue == '0') {
              this.$confirm('数据格式不规范?', '提示', {
                type: 'warning'
              })
            } else if (this.istrue == '1') {
              this.isAddMembers = false
            }
          })
        }
        setTimeout(() => {
          this.queryDict()
        }, 200)
      },
      /**
       * @author hpx
       * @date 2021/9/24 8:58
       * 保存并提交修改数据
       */
      sumbitEditRow() {
        if (this.addForm.dictionary_name === '' || this.addForm.dictionary_code === '' || this.addForm.create_by_name === '') {
          this.$confirm('请输入完整内容?', '提示', {
            type: 'warning'
          })
        }else {
          this.$axios
            .post('http://localhost:8443/updateDict', this.updateForm).then(resp => {
            this.sysUser = resp.data.sysUser
            this.isvalidate = resp.data.isvalidate
            if (this.isvalidate == '0') {
              this.$confirm('数据格式不规范?', '提示', {
                type: 'warning'
              })
            } else if (this.isvalidate == '1') {
              this.centerDialogVisible = false
            }
          })
          setTimeout(() => {
            this.queryDict()
          }, 200)
        }
      },
      /**
       * @author hpx
       * @date 2021/9/24 14:01
       * 删除数据
       */
      removeDict(id) {
        this.$confirm('此操作将永久删除该文件?', '提示', {
          type: 'warning'
        }).then(() => {
          this.$axios
            .post('http://localhost:8443/deleteDict', {
              dictionary_id: id
            }).then(resp => {
            if (resp && resp.status === 200) {
              this.queryDict()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      /**
       * @author hpx
       * @date 2021/9/24 8:58
       * 关闭弹框
       */
      closeDialog() {
        this.centerDialogVisible = false
        this.isAddMembers = false
        this.reload() // 刷新页面
      },
      /**
       * @author hpx
       * @date 2021/9/24 8:58
       * 搜索框显示隐藏
       */
      changeIsShow() {
        this.isShow = !this.isShow
      },
      /**
       * @author hpx
       * @date 2021/9/24 8:58
       * 刷新表格
       */
      refresh() {
        this.reload()
      }
    }
  }
</script>
<style scoped>
  .contents{
    width: auto;
    display: flex;
    align-items: flex-start;
  }
  .head-container,.center-right{
    width: 45%;
    border-radius: 4px;
    box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
    border: 1px solid #e6ebf5;
    background-color: #fff;
    color: #303133;
  }
  .head-container{
    padding: 20px;
  }
  .center-right{
    width: 52%;
    overflow: hidden;
    margin-left: 10px;
  }
  .el-col-lg-20 {
    width: 100%;
  }
  .center-con /deep/ thead th:first-child .el-checkbox{
    visibility: hidden;
  }
  .content-text{
    padding: 20px;
  }
  .el-button--mini, .el-button--mini.is-round {
    padding: 7px;
    float: right;
  }
  .content-top{
    border-bottom: 1px solid #e6ebf5;
    padding: 15px;
    font-size: 14px;
  }
  .content-top h3{
    display: inline;
  }
  .content-bottom{
    padding-top: 20px;
  }
  .btn_x{
    display: inline-block;
    width: 40px;
    height: 40px;
    background: #F56C6C;
    border-radius: 50%;
    margin-left: 10px;
    color: white;
  }
</style>
